#include "Http.hpp"
#include <iostream>
#include <vector>
#include "../include/Buffer.h"
void Print(std::vector<std::string> &s)
{
    for (auto &x : s)
        std::cout << "[" << x << "]" << std::endl;
}
void test1()
{
    std::string temp1 = "abc,def,,,ghj,";
    std::string temp2 = "abc,def,,,ghj";

    std::vector<std::string> data1;
    size_t x = Util::Split(temp1, ",", &data1);
    Print(data1);

    std::vector<std::string> data2;
    size_t x1 = Util::Split(temp2, ",", &data2);
    Print(data2);

    std::vector<std::string> data3;
    size_t x2 = Util::Split("abc", ",", &data3);
    Print(data3);
}
void test2()
{
    std::string str;
    Util::ReadFile("/home/wk/Code/project-c/HighConcurrencyServer/Http/main.cc", str);
    Util::WriteFile("/home/wk/Code/project-c/HighConcurrencyServer/Http/1.txt", "hello\r\nhedasdallo");
    std::string res = Util::EnCodeUrl("/login?user&  password=c++e", true);
    std::cout << res << std::endl;

    std::string res1 = Util::DeCodeUrl(res, true);
    std::cout << res1 << std::endl;
    std::cout << Util::StatuCodeDesc(404) << std::endl;
    std::cout << Util::StatuCodeDesc(101) << std::endl;
    std::cout << Util::StatuCodeDesc(502) << std::endl;
    std::cout << Util::StatuCodeDesc(401) << std::endl;
    std::cout << Util::StatuCodeDesc(432) << std::endl;

    std::cout << Util::ExtensionMime("a.b.txt") << std::endl;
    std::cout << Util::ExtensionMime("a") << std::endl;
    std::cout << Util::ExtensionMime("a.html") << std::endl;
    std::cout << Util::ExtensionMime("a.b.") << std::endl;
    std::cout << Util::ExtensionMime("a.b.java") << std::endl;
    std::cout << Util::ExtensionMime("a.b.cs") << std::endl;

    std::string p1 = "/home/wk";
    std::string p2 = "/home/wk/Code/project-c/HighConcurrencyServer/1.txt";

    std::cout << "IsDirectory(" << p1 << "): " << Util::IsDirectory(p1) << std::endl;
    std::cout << "IsRegular(" << p2 << "): " << Util::IsRegular(p2) << std::endl;

    std::cout << Util::VaildPath("/index.html") << std::endl;
    std::cout << Util::VaildPath("/../index.html") << std::endl;
    std::cout << Util::VaildPath("/../../index.html") << std::endl;
    std::cout << Util::VaildPath("/index.html/../../hello") << std::endl;

    std::cout << "===========================================" << std::endl;
}

std::string RequestStr(HttpRequest req)
{
    std::stringstream ss;
    ss << req._req_method << " " << req._res_path << " " << req._protocol_ver << "\r\n";
    for (auto &it : req._head_field)
    {
        ss << it.first << ": " << it.second << "\r\n";
    }

    for (auto &it : req._query_str)
    {
        ss << it.first << ": " << it.second << "\r\n";
    }

    ss << "\r\n";
    ss << req._content;

    return ss.str();
}

void hello(const HttpRequest &req, HttpResponse &res)
{
    res.InsertContent(RequestStr(req), "text/plain");
}

void Login(const HttpRequest &req, HttpResponse &res)
{
    res.InsertContent(RequestStr(req), "text/plain");
}

void PutFile(const HttpRequest &req, HttpResponse &res)
{
    // res.InsertContent(RequestStr(req), "text/plain");
    std::string pathname = "/home/wk/Code/project-c/HighConcurrencyServer/root" + req._res_path;
    Util::WriteFile(pathname, req._content);
}

void Delfile(const HttpRequest &req, HttpResponse &res)
{
    res.InsertContent(RequestStr(req), "text/plain");
}

void test3()
{
    HttpServer server(8080);

    server.setBaseDir("/home/wk/Code/project-c/HighConcurrencyServer/root");
    server.setThreadCount(4);
    server.Get("/hello", hello);
    server.Post("/login", Login);
    server.Put("/1234.txt", PutFile);
    server.Delete("/1234.txt", Delfile);
    server.Listen();
}

int main()
{

    test3();
    return 0;
}